class: title-slide, left, bottom # Introduction to R and Rstudio ---- ## **Session 1** ### ### --- class: center, middle # Agenda Using R Studio <br/> <br/> Importing data <br/> <br/> --------- Break --------- <br/> <br/> Introduction to ggplot2 <br/> <br/> What does this function do? <br/> <br/> --------- Lunch ----------- <br/> <br/> Data wrangling with dplyr <br/> <br/> Naming objects | Relational data <br/> <br/> --------- Break --------- <br/> <br/> R Markdown <br/> <br/> Ongoing learning <br/> <br/> .green[Finish about 4 - 430pm] <br/> --- class: center, middle # Course Aims </br> ####1. To show you some of the possibilities: ####2. To give you a feel for how R works. ####3. To show you enough for you to begin teaching yourself _(Excellent free resources available)_ --- class: inverse, middle, center .left-col[.center[ ###_elegant_ #Graphics ]] --- [<img class="center" src="data:image/png;base64,#img/session01/heatmap_colin_angus.PNG"/>](https://github.com/VictimOfMaths/COVID-19) --- The original blog is no longer available that featured this <img class="center" src="data:image/png;base64,#img/session01/london_cycle_routes.PNG" width="90%"/> --- class: center, middle # Collaboration <br/> <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"></path></svg> ## Reproducibility <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M413.1 222.5l22.2 22.2c9.4 9.4 9.4 24.6 0 33.9L241 473c-9.4 9.4-24.6 9.4-33.9 0L12.7 278.6c-9.4-9.4-9.4-24.6 0-33.9l22.2-22.2c9.5-9.5 25-9.3 34.3.4L184 343.4V56c0-13.3 10.7-24 24-24h32c13.3 0 24 10.7 24 24v287.4l114.8-120.5c9.3-9.8 24.8-10 34.3-.4z"></path></svg> ### R Markdown --- <img class="center" src="data:image/png;base64,#img/session01/automated_reports.PNG"/> --- # (Interactive) Dashboards [Mental Health Surge Modelling](https://strategyunit.shinyapps.io/MH_Surge_Modelling/) <img src="data:image/png;base64,#img/session01/mental_health_modelling.PNG"/> --- [Trafford Data Lab](https://trafforddatalab.shinyapps.io/trafford-tweet-dash/) and the main [site](https://www.trafforddatalab.io/) <img src="data:image/png;base64,#img/session01/twitter_dash.PNG"/> --- # R to SQL connection [NHS-R Community Webinar](https://nhsrcommunity.com/learn-r/workshops/database-connections-in-r-webinar) <img src="data:image/png;base64,#img/session01/webinar_nhsr.PNG"/> --- class: center, middle # Inclusivity <!-- --> --- class: inverse, middle, center .left-col[.center[ _elegant_ ## Course Philosophy ]] --- [Minimum Viable Product](https://blog.crisp.se/2016/01/25/henrikkniberg/making-sense-of-mvp) .left-col[.center[ <img src="data:image/png;base64,#img/session01/mvp.PNG" width="75%"/> ]] --- class: center, middle # Course philosophy </br> Relaxed and informal </br> </br> Slides and code are available on [<svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> original](https://github.com/nhs-r-community/intro_r) and [<svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> updated](https://github.com/Lextuga007/intro_r/tree/feature_conversion)) </br> </br> The truth; but it can’t be the whole truth… </br> </br> too much to cover in a day</br> --- class: inverse, middle, center # Let's begin --- class: center, middle ## R vs. RStudio </br> R is a programming language </br> </br> RStudio is a software application with tools to </br> </br> improve your programming experience --- <img class="center" src="data:image/png;base64,#img/session01/race_car.PNG"/> --- <img class="center" src="data:image/png;base64,#img/session01/roberto-nickson-unsplash.PNG"/> --- # RStudio </br> </br> Many excellent features to help you with your analyses. </br> </br> Never again have to think about R and RStudio as separate: Opening R Studio opens an R session. </br> </br> Analogy from the book Modern Dive: [www.moderndive.com](www.moderndive.com) --- class: center, middle # Open RStudio </br> </br> R Studio opens an R session. --- .left-column[ The Console is your window to R. </br> </br> You can code directly in the console… </br> </br> pi*2 <kbd> Enter </kbd> </br> </br> 37/12 <kbd> Enter <kbd> </br> </br> … but there is a better way… ] .right-column[ <img src="data:image/png;base64,#img/session01/rstudio_console.PNG"/> ] --- .left-column[ #### The Editor </br> </br> If you don’t see the Editor pane, click top right button </br> </br> And choose "R Script" from the drop down. </br> </br> Or, shortcut: </br> <kbd> Ctrl + Shift + N </kbd> </br> </br> .blue[_The cloud shortcut is <kbd> Ctrl + Shift + Alt + N </kbd>_] ] .pull-right[ <img src="data:image/png;base64,#img/session01/new_file.PNG"/> ] --- .left-column[ The Editor is just like any other text editor: you can copy, paste, and save text. </br> </br> <kbd> Ctrl + Z </kbd> undoes *but* </br> <kbd> Ctrl + Shift + Z </kbd> to redo </br> (_not <kbd> Ctrl + y</kbd>_) </br> Different text is coloured (the console is uniform) </br> </br> Autocomplete </br> </br> <kbd>Ctrl + Enter </kbd> (sends line of code to Console) ] .right-column[ <img src="data:image/png;base64,#img/session01/editor.PNG"/> ] --- .left-column[ Comment code with a # </br> </br> E.g # this was a bad idea Comment frequently, at least in the beginning ] .right-column[ <img src="data:image/png;base64,#img/session01/editor.PNG"/> ] --- # Tools -> Options [Reasons why this is default](https://community.rstudio.com/t/defaults-of-saving-and-restoring-workspace/939) <img class="center" src="data:image/png;base64,#img/session01/global_options.PNG"/> --- # Tools -> Options Accessbility and comfort for all <img class="center" src="data:image/png;base64,#img/session01/appearance.PNG"/> --- class: center, middle # Packages </br> ### R packages are like apps for your phone: </br> -- </br> Extend the capabilities of the basic or "base R" </br> with extra functions, datasets, documentation. --- <img class="center" src="data:image/png;base64,#img/session01/app_analogy.PNG"/> --- # Packages </br> will download a package to your personal library. Then: </br> This tells R to load the package from your personal library and is _needed for every new session/script_] --- class: center, middle ## CRAN repository </br> [Comprehensive R Archive Network](https://cran.r-project.org/web/packages/) </br> 17,306 packages (March 20201). Free. Peer reviewed. </br> </br> (Manifold possibilities) eg. interactive graphics and </br> </br> dashboards, machine learning, mine twitter data, </br> </br> create PowerPoint docs, maps… </br> </br> </br> </br>.blue[Other ways to get packages (eg. GitHub <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg>)] --- <img class="center" src="data:image/png;base64,#img/session01/tidyverse.PNG"/> --- class: center, middle ## What is the tidyverse? </br> </br> The [tidyverse package](https://www.tidyverse.org/) collects (some of) the most popular </br> R packages into one. </br> </br> All have the same underlying principles: </br> </br> Provide simple tools (with consistent structure) which may </br> be used together to help solve complex problems. --- class: center, middle ## What is the tidyverse? </br> </br> During the workshop we will use the [ggplot2](https://ggplot2.tidyverse.org/), [dplyr](https://dplyr.tidyverse.org/), </br> and [readr](https://readr.tidyverse.org/) packages. These are bundled up in the </br> tidyverse package. Load it by running: </br> </br> --- # Output Information - what was loaded and potential conflicts ``` -- Attaching packages ------------------------------------------ tidyverse 1.3.0 -- v ggplot2 3.3.3 v purrr 0.3.4 v tibble 3.1.0 v dplyr 1.0.4 v tidyr 1.1.2 v stringr 1.4.0 v readr 1.4.0 v forcats 0.5.1 -- Conflicts --------------------------------------------- tidyverse_conflicts() -- x dplyr::filter() masks stats::filter() x dplyr::lag() masks stats::lag() ``` Warnings - not errors, just information on versions ``` Warning messages: 1: package ‘tidyverse’ was built under R version 4.0.4 2: package ‘tidyr’ was built under R version 4.0.3 3: package ‘readr’ was built under R version 4.0.3 4: package ‘purrr’ was built under R version 4.0.3 5: package ‘dplyr’ was built under R version 4.0.3 6: package ‘stringr’ was built under R version 4.0.3 ``` --- #### This work is licensed as </br> Creative Commons </br> Attribution </br> ShareAlike 4.0 </br> International </br> To view a copy of this license, visit </br> https://creativecommons.org/licenses/by/4.0/ --- name: goodbye class: middle, inverse # **Thank you!** <br/> Acknowledgements: for creating the original training slides and delivering training: Andrew Jones | Ozayr Mohammed <br/> Healthcare Analysts | The Strategy Unit [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> andrew.jones40@nhs.net](mailto:andrew.jones40@nhs.net)<br/> [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> ozayr.mohammed@nhs.net](mailto:ozayr.mohammed@nhs.net) And to Silvia Canelón who created the Xaringan presentation using NHS and NHR-R colour branding and shared this at the 2020 [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html). <br/> Details of the workshops she ran at the [NHS-R Community conference](https://spcanelon.github.io/xaringan-basics-and-beyond/index.html). And to Zoë Turner who converted the presentation to Xaringan. [<svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M440 6.5L24 246.4c-34.4 19.9-31.1 70.8 5.7 85.9L144 379.6V464c0 46.4 59.2 65.5 86.6 28.6l43.8-59.1 111.9 46.2c5.9 2.4 12.1 3.6 18.3 3.6 8.2 0 16.3-2.1 23.6-6.2 12.8-7.2 21.6-20 23.9-34.5l59.4-387.2c6.1-40.1-36.9-68.8-71.5-48.9zM192 464v-64.6l36.6 15.1L192 464zm212.6-28.7l-153.8-63.5L391 169.5c10.7-15.5-9.5-33.5-23.7-21.2L155.8 332.6 48 288 464 48l-59.4 387.3z"></path></svg> zoe.turner2@notthshc.nhs.uk](mailto:zoe.turner2@nottshc.nhs.uk)